﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

/// <summary>
/// 把Resource下的资源打包成.data到OutPut目录下
/// </summary>
public class BoundleBuilder : Editor
{
    public class AssetBundleImporter : AssetPostprocessor
    {

        //// 全部资源加载完毕之后调用
        //static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        //{
        //    foreach (var item in importedAssets)
        //    {
        //          var importer = AssetImporter.GetAtPath(item);
        //          importer.assetBundleName = "data";
        //    }
        //}
    }
    public class CreateAssetBundles
    {
        //在Unity编辑器中添加菜单
        [MenuItem("菜单/单个资源打包Android>>>")]
        static void ExportResource_Android()
        {


            if (Selection.objects == null || Selection.objects.Length < 1)
                return;

            int NNN =Selection.objects.Length;

            string dir = Application.streamingAssetsPath + "/MAB_AssetBundle_Android";// "AssetBundles";
                                                                    //判断该目录是否存在
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
            }

            //备打文件集合，当前为单个
            AssetBundleBuild[] abb = new AssetBundleBuild[NNN];

            for( int i=0 ;i<NNN;i++)
            {
                //文件路径（当前为单个，多个文件使用数组的形式）
                string[] path = new string[1];
                //获取文件路径（得到鼠标选中的文件）；
                path[0] = AssetDatabase.GetAssetPath(Selection.objects[i]);
                //备打文件路径名，从Asset/开始
                abb[i].assetNames = path;
                //包名
                abb[i].assetBundleName = Selection.objects[i].name;
                abb[i].assetBundleVariant = "mab";
            }



            //打包（“输出路径”，备打文件集合，打包设置，目标平台）
            BuildPipeline.BuildAssetBundles(dir, abb, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.Android);

            AssetDatabase.Refresh();
        }
        //在Unity编辑器中添加菜单
        [MenuItem("菜单/单个资源打包IOS")]
        static void ExportResource_IOS()
        {
            if (Selection.objects == null || Selection.objects.Length < 1)
                return;
            string dir = Application.streamingAssetsPath + "/MAB_AssetBundle_IOS";// "AssetBundles";
                                                                              //判断该目录是否存在
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
            }

            //备打文件集合，当前为单个
            AssetBundleBuild[] abb = new AssetBundleBuild[Selection.objects.Length];
            for (int i = 0; i < Selection.objects.Length; i++)
            {
                abb[i].assetNames = new string[1] { AssetDatabase.GetAssetPath(Selection.objects[i]) };
                //包名
                abb[i].assetBundleName = Selection.objects[i].name;
                abb[i].assetBundleVariant = "mab";

            }
            ////文件路径（当前为单个，多个文件使用数组的形式）
            //string[] path = new string[1];
            ////获取文件路径（得到鼠标选中的文件）；
            //path[0] = AssetDatabase.GetAssetPath(Selection.activeObject);
            ////备打文件路径名，从Asset/开始


            //打包（“输出路径”，备打文件集合，打包设置，目标平台）
            BuildPipeline.BuildAssetBundles(dir, abb, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.iOS);

            AssetDatabase.Refresh();
        }

        [MenuItem("菜单/资源打包")]
        static void BuildAllExAssetBundles()//进行打包
        {
            string dir = Application.streamingAssetsPath + "/MAB_AssetBundle";// "AssetBundles";
                                                                    //判断该目录是否存在
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
            }

            BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
            //刷新
            AssetDatabase.Refresh();
        }
        [MenuItem("菜单/资源打包_Android")]
        static void BuildAllExAssetBundles_Android()//进行打包
        {
            string dir = Application.streamingAssetsPath + "/MAB_AssetBundle_Android";// "AssetBundles";
                                                                        //判断该目录是否存在
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
            }

            BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.Android);
            //刷新
            AssetDatabase.Refresh();
        }
        [MenuItem("菜单/资源打包_IOS")]
        static void BuildAllExAssetBundles_IOS()//进行打包
        {
            string dir = Application.streamingAssetsPath + "/MAB_AssetBundle_IOS";// "AssetBundles";
                                                                    //判断该目录是否存在
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
            }

            BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.iOS);
            //刷新
            AssetDatabase.Refresh();
        }

    }
 }

public class Platform
{
    public static string GetPlatformFolder(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.Android:
                return "Android";
            case BuildTarget.iOS:
                return "IOS";
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                return "Windows";
            default:
                return null;
        }
    }
}